package com.franklin.ideaplugin.easytesting.ui.execute;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.franklin.ideaplugin.api.beans.FileClass;
import com.franklin.ideaplugin.api.utils.SwingUtils;
import com.franklin.ideaplugin.easytesting.ui.Sizes;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.psi.PsiParameter;
import com.intellij.ui.components.JBTextField;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.util.Objects;

/**
 * @author Ye Junhui
 * @since 2023/7/2 16:51
 */
public class FileClassParameterFormProvider implements IParameterFormProvider {
    @Override
    public boolean isTarget(@NotNull Object defaultValue) {
        return defaultValue instanceof FileClass;
    }

    @Override
    public ParameterTextComponent getForm(PsiParameter psiParameter, Object defaultValueOfPsiType, Object defaultValue) {
        FileClass fileClass = (FileClass) defaultValueOfPsiType;
        if (Objects.nonNull(defaultValue) && defaultValue instanceof String){
            fileClass = JSON.parseObject(defaultValue.toString(),FileClass.class);
        }
        if (Objects.isNull(fileClass)){
            fileClass = (FileClass) defaultValueOfPsiType;
        }

        //按钮
        JButton button = new JButton(AllIcons.Actions.MenuOpen);
        //路径文本
        JBTextField textField = new JBTextField(fileClass.getFilePath());

        JPanel jPanel = new JPanel();
        jPanel.setMaximumSize(Sizes.ExecuteMethodFormSize.CHILD_SIZE);
        jPanel.setPreferredSize(Sizes.ExecuteMethodFormSize.CHILD_SIZE);
        jPanel.setLayout(SwingUtils.createLayoutManager(1,2));
        SwingUtils.addCell(jPanel,button,0,0,new Dimension(20,20));
        SwingUtils.addCell(jPanel,textField,0,1,new Dimension(170,20));

        FileClass finalFileClass1 = fileClass;
        button.setToolTipText("Select a file for uploading");
        button.addActionListener(e -> {
            VirtualFile originFile = null;
            if (StrUtil.isNotBlank(finalFileClass1.getFilePath())){
                File file = FileUtil.file(finalFileClass1.getFilePath());
                if (file.exists()){
                    VirtualFileManager virtualFileManager = VirtualFileManager.getInstance();
                    originFile = virtualFileManager.findFileByNioPath(file.toPath());
                }
            }
            VirtualFile file = FileChooser.chooseFile(
                    new FileChooserDescriptor(
                            true,
                            false,
                            false,
                            true,
                            false,
                            false),
                    psiParameter.getProject(),
                    originFile
            );
            if (Objects.nonNull(file)) {
                textField.setText(file.getCanonicalPath());
            }
        });
        FileClass finalFileClass = fileClass;
        return new ParameterTextComponent(jPanel) {
            @Override
            public String getText() {
                FileClass newFile = new FileClass();
                newFile.setFilePath(textField.getText());
                newFile.setQualifiedName(finalFileClass.getQualifiedName());
                return newFile.toJsonString();
            }

            @Override
            public String getTypeStr(PsiParameter psiParameter) {
                return com.franklin.ideaplugin.easytesting.common.entity.FileClass.class.getCanonicalName();
            }
        };
    }

}
